public class Solution3 {
    //152. 乘积最大子数组
    public int maxProduct(int[] nums) {
        int n = nums.length;
        int[] f = new int[n]; //以i结尾的乘积最大的子数组
        int[] g = new int[n]; //以i结尾的乘积最小的子数组
        f[0] = g[0] = nums[0];
        int ret = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i] > 0) {
                f[i] = Math.max(f[i-1]* nums[i],nums[i]);
                g[i] = Math.min(g[i-1]*nums[i],nums[i]);
                ret = Math.max(ret,f[i]);
            } else {
                f[i] = Math.max(g[i-1]*nums[i],nums[i]);
                g[i] = Math.min(f[i-1]*nums[i],nums[i]);
                ret = Math.max(ret,f[i]);
            }
        }
        return ret;
    }
}
